package com.neusoft.neuiotms.dcom.controller.alter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.file.dto.FileDTO;
import com.neusoft.bizcore.file.service.FileService;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.neuiotms.dcom.dto.alter.AlterApplyFormDTO;
import com.neusoft.neuiotms.dcom.dto.alter.AlterCountersignatureApprovalDTO;
import com.neusoft.neuiotms.dcom.dto.alter.AlterCustomerReviewDTO;
import com.neusoft.neuiotms.dcom.dto.alter.AlterFrontlineProcessingFormDTO;
import com.neusoft.neuiotms.dcom.dto.alter.AlterLeaderApprovalDTO;
import com.neusoft.neuiotms.dcom.dto.alter.AlterSecondLeaderDTO;
import com.neusoft.neuiotms.dcom.dto.alter.AlterSecondLineDTO;
import com.neusoft.neuiotms.dcom.service.alter.AlterApplyFormService;
import com.neusoft.neuiotms.dcom.service.alter.AlterCountersignatureApprovalService;
import com.neusoft.neuiotms.dcom.service.alter.AlterCustomerReviewService;
import com.neusoft.neuiotms.dcom.service.alter.AlterFrontlineProcessingFormService;
import com.neusoft.neuiotms.dcom.service.alter.AlterLeaderApprovalService;
import com.neusoft.neuiotms.dcom.service.alter.AlterSecondLeaderService;
import com.neusoft.neuiotms.dcom.service.alter.AlterSecondLineService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "变更管理")
@RestController
@RequestMapping(value = "/api/w/alter")
public class AlterController {

    @Autowired
    private AlterApplyFormService applyFormService;
    @Autowired
    private AlterFrontlineProcessingFormService alterFrontlineProcessingFormService;
    @Autowired
    private AlterLeaderApprovalService leaderApprovalService;
    @Autowired
    private AlterCustomerReviewService customerReviewService;
    @Autowired
    private AlterSecondLineService secondLineService;
    @Autowired
    private AlterCountersignatureApprovalService countersignatureApprovalService;
    @Autowired
    private AlterSecondLeaderService secondLeaderService;
    @Autowired
    private FileService fileService;

    @ApiOperation("新增故障申请表单")
    @PostMapping("/applyform")
    public ResultDTO<AlterApplyFormDTO> create(@RequestBody final AlterApplyFormDTO dto,
            final UserBean user) {
        return ResultDTO.success(this.applyFormService.create(dto, user));
    }

    @ApiOperation("查看故障申请表单")
    @GetMapping("/applyform/{workNum}/{taskId}")
    public ResultDTO<AlterApplyFormDTO> details(@PathVariable final String workNum,
            @PathVariable final String taskId,
            final UserBean user) {
        return ResultDTO.success(this.applyFormService.detail(workNum, taskId, user));
    }

    @ApiOperation("申请annex上传文件")
    @PostMapping("applyform/upload/annex")
    public ResultDTO<FileDTO> applyformUpload(@RequestParam("file") final MultipartFile file) {
        final FileDTO doc = this.applyFormService.save(file);
        return ResultDTO.success(doc);
    }

    @ApiOperation("申请annex下载文件")
    @GetMapping("applyform/download/annex/{fsId}")
    public void applyformDownload(@PathVariable final String fsId, final HttpServletResponse response) {
        this.fileService.download(fsId, response);
    }

    @ApiOperation("申请annex删除文件")
    @DeleteMapping("applyform/annex/{fsId}")
    public void applyformDeleteAttachments(@PathVariable final String fsId) {
        this.applyFormService.deleteMongoFile(fsId);
    }

    @ApiOperation("新增故障处理表单")
    @PostMapping("/frontlineProcessingForm")
    public ResultDTO<AlterFrontlineProcessingFormDTO> create(
            @RequestBody final AlterFrontlineProcessingFormDTO dto,
            final UserBean user) {
        return ResultDTO.success(this.alterFrontlineProcessingFormService.create(dto, user));
    }

    @ApiOperation("查看故障处理表单")
    @GetMapping("/frontlineProcessingForm/{workNum}/{taskId}")
    public ResultDTO<AlterFrontlineProcessingFormDTO> frontlineProcessingDetails(
            @PathVariable final String workNum,
            @PathVariable final String taskId,
            final UserBean user) {
        return ResultDTO.success(this.alterFrontlineProcessingFormService.detail(workNum, taskId, user));
    }

    @ApiOperation("新增组长审批意见")
    @PostMapping("/leaderapproval")
    public ResultDTO<AlterLeaderApprovalDTO> create(@RequestBody final AlterLeaderApprovalDTO dto,
            final UserBean user) {
        return ResultDTO.success(this.leaderApprovalService.create(dto, user));
    }

    @ApiOperation("查看组长审批意见")
    @GetMapping("/leaderapproval/{workNum}/{taskId}")
    public ResultDTO<AlterLeaderApprovalDTO> leaderApprovalDetails(@PathVariable final String workNum,
            @PathVariable final String taskId,
            final UserBean user) {
        return ResultDTO.success(this.leaderApprovalService.detail(workNum, taskId, user));
    }

    @ApiOperation("新增二线领导确认")
    @PostMapping("/secondLeader")
    public ResultDTO<AlterSecondLeaderDTO> secondLeaderCreate(@RequestBody final AlterSecondLeaderDTO dto,
            final UserBean user) {
        return ResultDTO.success(this.secondLeaderService.create(dto, user));
    }

    @ApiOperation("查看二线领导确认")
    @GetMapping("/secondLeader/{workNum}/{taskId}")
    public ResultDTO<AlterSecondLeaderDTO> secondLeaderDetails(@PathVariable final String workNum,
            @PathVariable final String taskId,
            final UserBean user) {
        return ResultDTO.success(this.secondLeaderService.detail(workNum, taskId, user));
    }

    @ApiOperation("新增二线处理")
    @PostMapping("/secondLine/{type}")
    public ResultDTO<AlterSecondLineDTO> secondLineCreate(@RequestBody final AlterSecondLineDTO dto,
            @PathVariable final String type,
            final UserBean user) {
        return ResultDTO.success(this.secondLineService.create(dto, type, user));
    }

    @ApiOperation("查看二线处理")
    @GetMapping("/secondLine/{workNum}/{taskId}")
    public ResultDTO<AlterSecondLineDTO> secondLineDetails(@PathVariable final String workNum,
            @PathVariable final String taskId,
            final UserBean user) {
        return ResultDTO.success(this.secondLineService.detail(workNum, taskId, user));
    }

    @ApiOperation("新增会签节点处理")
    @PostMapping("/countersignature")
    public ResultDTO<AlterCountersignatureApprovalDTO> countersignatureCreate(
            @RequestBody final AlterCountersignatureApprovalDTO dto,
            final UserBean user) {
        return ResultDTO.success(this.countersignatureApprovalService.create(dto, user));
    }

    @ApiOperation("查看会签节点处理")
    @GetMapping("/countersignature/{workNum}/{taskId}")
    public ResultDTO<AlterCountersignatureApprovalDTO> countersignatureDetails(@PathVariable final String workNum,
            @PathVariable final String taskId,
            final UserBean user) {
        return ResultDTO.success(this.countersignatureApprovalService.detail(workNum, taskId, user));
    }

    @ApiOperation("新增客户评价")
    @PostMapping("/customerreview")
    public ResultDTO<AlterCustomerReviewDTO> customerReviewCreate(
            @RequestBody final AlterCustomerReviewDTO dto,
            final UserBean user) {
        return ResultDTO.success(this.customerReviewService.create(dto, user));
    }

    @ApiOperation("查看客户评价")
    @GetMapping("/customerreview/{workNum}/{taskId}")
    public ResultDTO<AlterCustomerReviewDTO> customerReviewDetails(@PathVariable final String workNum,
            @PathVariable final String taskId,
            final UserBean user) {
        return ResultDTO.success(this.customerReviewService.detail(workNum, taskId, user));
    }

    @ApiOperation("annex上传文件")
    @PostMapping("customerreview/upload/annex")
    public ResultDTO<FileDTO> upload(@RequestParam("file") final MultipartFile file) {
        final FileDTO doc = this.customerReviewService.save(file);
        return ResultDTO.success(doc);
    }

    @ApiOperation("annex下载文件")
    @GetMapping("customerreview/download/annex/{fsId}")
    public void download(@PathVariable final String fsId, final HttpServletResponse response) {
        this.fileService.download(fsId, response);
    }

    @ApiOperation("annex删除文件")
    @DeleteMapping("customerreview/annex/{fsId}")
    public void deleteAttachments(@PathVariable final String fsId) {
        this.customerReviewService.deleteMongoFile(fsId);
    }

}
